﻿using System;
using System.IO;

namespace Thuoc24h.Home.Pages
{
    public partial class BuyMedicineByForm : BasePage.BasePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lbmess.Visible = false;
        }

        protected void btnSend_OnClick(object sender, EventArgs e)
        {
            string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            string exten = extension.ToLower();
                CaptchaControl1.ValidateCaptcha(txtCaptcha.Text.Trim());
            if (CaptchaControl1.UserValidated)
            {
            
            if (exten.Equals(".jpg") || exten.Equals(".jpeg") || exten.Equals(".png") || exten.Equals(".txt") || exten.Equals(".doc") || exten.Equals(".docx") || exten.Equals(".exl") || exten.Equals("exls"))
            {


                string filename = "";

                if (FileUpload1.HasFile)
                {
                    try
                    {
                        filename = Path.GetFileName(FileUpload1.FileName);
                        FileUpload1.SaveAs(Server.MapPath("~/Upload/Documents/") + filename);
                        //   StatusLabel.Text = "Upload status: File uploaded!";
                    }
                    catch (Exception ex)
                    {
                        //  StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }


                var str = "";
                var txtThuoc = Request.Form["txtListThuoc"];
                if (txtThuoc != string.Empty)
                {
                    var lists = txtThuoc.Split(';');
                    foreach (var item in lists)
                    {
                        str += "-" + item + "<br />";
                    }
                }

                var attack = "";
                attack = "<a href=\"/Upload/Documents/" + filename + "\">File đính kèm</a>";
                var contact = new Data.Contact
                {
                    Name = txtName.Text,
                    Email = txtEmail.Text,
                    Address = txtAddress.Text,
                    PhoneNumber = txtPhone.Text,

                    Detail = attack + str + txtDetail.Text,
                    CreatedDate = DateTime.Now,
                    IsRead = false,
                    TypeRequest = 2

                };
                context.Contacts.AddObject(contact);
                int save = context.SaveChanges();
                if (save > 0)
                {

                    txtName.Text = txtEmail.Text = txtAddress.Text = txtPhone.Text = txtDetail.Text = string.Empty;
                    ClientScript.RegisterStartupScript(this.GetType(), "Thông báo", "alert('" + "Gửi yêu cầu thành công" + "');", true);
                }
            }
            else
            {
                lbmess.Visible = true;
                lbmess.Text = "Định dạng file hợp lệ: .jpg , .jpeg, .png, .doc, .docx, .exl, .exls.";
                return;

            }
            }


        }
    }
}